<template>
	<view class="content">
<!-- 		<u-navbar :is-back="false" title="美岸咖啡" title-color="#ffffff" :background="background">
			<view class="slot-wrap">
				
			</view>
		</u-navbar> -->
		<view class="u-page">
			<!-- 所有内容的容器 -->
			<view v-if='current == 0'>
				<home @func="getShopNumber"></home>
			</view>
			<view v-else-if='current == 1'>
				<shopping></shopping>
			</view>
			<view v-else='current == 2'>
				<user></user>
			</view>
		</view>
		<!-- #ifdef MP-WEIXIN -->
			<u-modal v-model="showAuth" 
				:show-confirm-button=false
				width ="400"
				:title-style="{color: '#dd6161'}"
				title="授权管理">
				<view class="slot-content">
					<view style="margin: 20rpx 10rpx;">
						我们将要获取你的授权
					</view>
					<u-row gutter="16">
						<u-col span="6">
							<u-button size="mini" style="float: right;" @click="showAuth? showAuth=false: showAuth=true">取消</u-button>
						</u-col>
						<u-col span="6">
							<u-button
								style="float: left;"
								size="mini"
								type="error"
								open-type="getUserInfo" 
								@getuserinfo="wxGetUserInfo" 
								withCredentials="true">允许
							</u-button>
						</u-col>
					</u-row>
				</view>
			</u-modal>
		<!-- #endif -->
	
		
		<!-- 与包裹页面所有内容的元素u-page同级，且在它的下方 -->
		<u-tabbar v-model="current" :list="list" :mid-button="true" :before-switch="beforeSwitch"></u-tabbar>
	</view>
</template>

<script>
	import home from "@/components/ui/home"; //首页
	import shopping from "@/components/ui/shopping"; //购物车
	import user from "@/components/ui/user"; //个人中心
	export default {
		components:{
			home,
			shopping,
			user
		},
		data() {
			return {
				background: {
					backgroundColor: '#dd6161',
					
					// 导航栏背景图
					// background: 'url(https://cdn.uviewui.com/uview/swiper/1.jpg) no-repeat',
					// 还可以设置背景图size属性
					// backgroundSize: 'cover',
					
					// 渐变色
					// backgroundImage: 'linear-gradient(45deg, rgb(28, 187, 180), rgb(141, 198, 63))'
				},
				list: [{
						iconPath: "home",
						selectedIconPath: "home-fill",
						text: '首页',
						count: 2,
						isDot: true,
						customIcon: false,
					},
					{
						iconPath: "https://cdn.uviewui.com/uview/common/min_button.png",
						selectedIconPath: "https://cdn.uviewui.com/uview/common/min_button_select.png",
						text: '购物车',
						count: 0,
						midButton: true,
						customIcon: false,
					},
					{
						iconPath: "account",
						selectedIconPath: "account-fill",
						text: '我的',
						isDot: false,
						customIcon: false,
					},
				],
				current: 0, //page的index
				count: 1, //记录购物车商品个数
				showAuth:true,
				number:"",
			}
		},
		onLoad(options) {
			console.log(options)
			// #ifndef H5
				// options 中的 scene 需要使用 decodeURIComponent 才能获取到生成二维码时传入的 scene
				var scene = decodeURIComponent(options.scene)
				console.log(scene)
				// console.log(options.number)
				// this.number = options.number
				this.$store.dispatch('updNumber',options.number); //设置桌号
			
			// #endif
		},
		onShow() {

		},
		methods: {
			beforeSwitch(index) {
				// console.log(index);
				this.current = index;
				// 只能切换偶数项
				if(index % 2 == 0){
					 return true;
				}else{
					return false;
				}
			},//购物车商品数量
			getShopNumber(val){
				this.count = this.$store.dispatch('addShopNumber');
				this.count = this.$store.state.ShopNumber;
				console.log('购物车添加'+this.count);
				this.list[1].count = this.count;
			},
			wxGetUserInfo:function(res){
/* 				if (!res.detail.iv) {
					uni.showToast({
						title: "您取消了授权,登录失败",
						icon: "none"
					});
					return false;
				}
				console.log('-------用户授权，并获取用户基本信息和加密数据------')
				console.log(res);
				var userInfo = JSON.parse(res.detail.rawData)
				console.log(userInfo);
				console.log(userInfo.nickName);
				console.log(userInfo.nickName);
				console.log(res.detail.signature);
				uni.showToast({
					title: "授权成功",
					icon: "none"
				}); */
				// 查看是否授权
				wx.getSetting({
				  success (res){
					if (res.authSetting['scope.userInfo']) {
					  // 已经授权，可以直接调用 getUserInfo 获取头像昵称
					  wx.getUserInfo({
						success: function(res) {
						  console.log(res.userInfo)
						}
					  })
					}
				  }
				});
				
				wx.login({
				  success (res) {
					  console.log(res.code)
						if (res.code) {
						  //发起网络请求
						  wx.request({
							url: getApp().globalData.app_url+"/api/Login",
							data: {
							  code: res.code
							}
						  })
						} else {
						  console.log('登录失败！' + res.errMsg)
						}
				  }
				});
				
				this.showAuth = false;
			}
		}
	}
</script>

<style lang="scss" scoped>


</style>
